package com.libbase.test;

import android.support.annotation.NonNull;
import android.support.v4.util.Pools;

import com.base.pool.FactoryPools;
import com.base.pool.StateVerifier;

/**
 * 对象池的test案例
 */
public class PoolBean implements FactoryPools.Poolable {

    private String name;

    private final StateVerifier mStateVerifier;

    private static final Pools.Pool<PoolBean> POOL = FactoryPools.simple(150,
            new FactoryPools.Factory<PoolBean>() {
                @Override
                public PoolBean create() {
                    return new PoolBean();
                }
            });


    public PoolBean() {
        mStateVerifier = StateVerifier.newInstance();
    }

    @NonNull
    @Override
    public StateVerifier getVerifier() {
        return mStateVerifier;
    }

    private void init(String name) {
        this.name = name;
    }

    public static class Builder {

        private String name;

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public PoolBean builder() {
            PoolBean poolBean = POOL.acquire();
            assert poolBean != null;
            poolBean.init(name);
            return poolBean;
        }
    }

    public static Builder obtainBuilder() {
        return new Builder();
    }
}
